package com.sugar.psych.assessment.module.business.controller.admin.userpsychassessmentrecord;

import com.sugar.psych.assessment.module.business.dal.dataobject.userpsychassessmentrecord.UserPsychQuestionRecordDO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.sugar.psych.assessment.framework.common.pojo.PageParam;
import com.sugar.psych.assessment.framework.common.pojo.PageResult;
import com.sugar.psych.assessment.framework.common.pojo.CommonResult;
import com.sugar.psych.assessment.framework.common.util.object.BeanUtils;
import static com.sugar.psych.assessment.framework.common.pojo.CommonResult.success;

import com.sugar.psych.assessment.framework.excel.core.util.ExcelUtils;

import com.sugar.psych.assessment.framework.apilog.core.annotation.ApiAccessLog;
import static com.sugar.psych.assessment.framework.apilog.core.enums.OperateTypeEnum.*;

import com.sugar.psych.assessment.module.business.controller.admin.userpsychassessmentrecord.vo.*;
import com.sugar.psych.assessment.module.business.dal.dataobject.userpsychassessmentrecord.UserPsychAssessmentRecordDO;
import com.sugar.psych.assessment.module.business.service.userpsychassessmentrecord.UserPsychAssessmentRecordService;

@Tag(name = "管理后台 - 用户心理测评记录")
@RestController
@RequestMapping("/business/user-psych-assessment-record")
@Validated
public class UserPsychAssessmentRecordController {

    @Resource
    private UserPsychAssessmentRecordService userPsychAssessmentRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建用户心理测评记录")
    @PreAuthorize("@ss.hasPermission('business:user-psych-assessment-record:create')")
    public CommonResult<Long> createUserPsychAssessmentRecord(@Valid @RequestBody UserPsychAssessmentRecordSaveReqVO createReqVO) {
        return success(userPsychAssessmentRecordService.createUserPsychAssessmentRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户心理测评记录")
    @PreAuthorize("@ss.hasPermission('business:user-psych-assessment-record:update')")
    public CommonResult<Boolean> updateUserPsychAssessmentRecord(@Valid @RequestBody UserPsychAssessmentRecordSaveReqVO updateReqVO) {
        userPsychAssessmentRecordService.updateUserPsychAssessmentRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户心理测评记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:user-psych-assessment-record:delete')")
    public CommonResult<Boolean> deleteUserPsychAssessmentRecord(@RequestParam("id") Long id) {
        userPsychAssessmentRecordService.deleteUserPsychAssessmentRecord(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除用户心理测评记录")
                @PreAuthorize("@ss.hasPermission('business:user-psych-assessment-record:delete')")
    public CommonResult<Boolean> deleteUserPsychAssessmentRecordList(@RequestParam("ids") List<Long> ids) {
        userPsychAssessmentRecordService.deleteUserPsychAssessmentRecordListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户心理测评记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:user-psych-assessment-record:query')")
    public CommonResult<UserPsychAssessmentRecordRespVO> getUserPsychAssessmentRecord(@RequestParam("id") Long id) {
        UserPsychAssessmentRecordDO userPsychAssessmentRecord = userPsychAssessmentRecordService.getUserPsychAssessmentRecord(id);
        return success(BeanUtils.toBean(userPsychAssessmentRecord, UserPsychAssessmentRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户心理测评记录分页")
    @PreAuthorize("@ss.hasPermission('business:user-psych-assessment-record:query')")
    public CommonResult<PageResult<UserPsychAssessmentRecordRespVO>> getUserPsychAssessmentRecordPage(@Valid UserPsychAssessmentRecordPageReqVO pageReqVO) {
        PageResult<UserPsychAssessmentRecordDO> pageResult = userPsychAssessmentRecordService.getUserPsychAssessmentRecordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UserPsychAssessmentRecordRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户心理测评记录 Excel")
    @PreAuthorize("@ss.hasPermission('business:user-psych-assessment-record:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserPsychAssessmentRecordExcel(@Valid UserPsychAssessmentRecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UserPsychAssessmentRecordDO> list = userPsychAssessmentRecordService.getUserPsychAssessmentRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户心理测评记录.xls", "数据", UserPsychAssessmentRecordRespVO.class,
                        BeanUtils.toBean(list, UserPsychAssessmentRecordRespVO.class));
    }

    // ==================== 子表（用户心理测评题目表） ====================

    @GetMapping("/user-psych-question-record/page")
    @Operation(summary = "获得用户心理测评题目表分页")
    @Parameter(name = "recordId", description = "测评记录编号")
    @PreAuthorize("@ss.hasPermission('business:user-psych-assessment-record:query')")
    public CommonResult<PageResult<UserPsychQuestionRecordDO>> getUserPsychQuestionRecordPage(PageParam pageReqVO,
                                                                                              @RequestParam("recordId") Long recordId) {
        return success(userPsychAssessmentRecordService.getUserPsychQuestionRecordPage(pageReqVO, recordId));
    }

    @PostMapping("/user-psych-question-record/create")
    @Operation(summary = "创建用户心理测评题目表")
    @PreAuthorize("@ss.hasPermission('business:user-psych-assessment-record:create')")
    public CommonResult<Long> createUserPsychQuestionRecord(@Valid @RequestBody UserPsychQuestionRecordDO userPsychQuestionRecord) {
        return success(userPsychAssessmentRecordService.createUserPsychQuestionRecord(userPsychQuestionRecord));
    }

    @PutMapping("/user-psych-question-record/update")
    @Operation(summary = "更新用户心理测评题目表")
    @PreAuthorize("@ss.hasPermission('business:user-psych-assessment-record:update')")
    public CommonResult<Boolean> updateUserPsychQuestionRecord(@Valid @RequestBody UserPsychQuestionRecordDO userPsychQuestionRecord) {
        userPsychAssessmentRecordService.updateUserPsychQuestionRecord(userPsychQuestionRecord);
        return success(true);
    }

    @DeleteMapping("/user-psych-question-record/delete")
    @Parameter(name = "id", description = "编号", required = true)
    @Operation(summary = "删除用户心理测评题目表")
    @PreAuthorize("@ss.hasPermission('business:user-psych-assessment-record:delete')")
    public CommonResult<Boolean> deleteUserPsychQuestionRecord(@RequestParam("id") Long id) {
        userPsychAssessmentRecordService.deleteUserPsychQuestionRecord(id);
        return success(true);
    }

    @DeleteMapping("/user-psych-question-record/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除用户心理测评题目表")
    @PreAuthorize("@ss.hasPermission('business:user-psych-assessment-record:delete')")
    public CommonResult<Boolean> deleteUserPsychQuestionRecordList(@RequestParam("ids") List<Long> ids) {
        userPsychAssessmentRecordService.deleteUserPsychQuestionRecordListByIds(ids);
        return success(true);
    }

	@GetMapping("/user-psych-question-record/get")
	@Operation(summary = "获得用户心理测评题目表")
	@Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:user-psych-assessment-record:query')")
	public CommonResult<UserPsychQuestionRecordDO> getUserPsychQuestionRecord(@RequestParam("id") Long id) {
	    return success(userPsychAssessmentRecordService.getUserPsychQuestionRecord(id));
	}

}